package com.onlinemarket.thrid.daoimpl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.web.filter.OncePerRequestFilter;

import com.onlinemarket.thrid.dao.SupplierDao;
import com.onlinemarket.thrid.domain.Supplier;
import com.onlinemarket.thrid.domain.WareHouse;

@Repository
public class SupplierDaoImpl implements SupplierDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private Supplier supplier;

	@Override
	public int insertSupplier(Supplier supplier) {
		String sql = "insert into supplier (supplier_name,supplier_address,supplier_telephone,supplier_contact) values(?,?,?,?)";
		int insertnumber = jdbcTemplate.update(sql, new Object[] { supplier.getSupplier_name(),
				supplier.getSupplier_address(),supplier.getSupplier_telephone(), supplier.getSupplier_contact()});
		return insertnumber;
	}

	@Override
	public int updateSupplier(int id) {
		String sql = "update supplier set supplier_name=?,supplier_address=?,supplier_telephone=?,supplier_contact=? where supplier_id=?";
		jdbcTemplate.update(sql, new Object[] {});
		return 0;
	}

	@Override
	public Supplier selectSupplierinfoByname(String suppliername) {
		String sql = "select * from supplier where supplier_name=?";
		List<Map<String, Object>> supplierlist = jdbcTemplate.queryForList(sql, new Object[] { suppliername });
		Iterator<Map<String, Object>> iterator = supplierlist.iterator();
		while (iterator.hasNext()) {
			Map<String, Object> map = iterator.next();
			supplier = new Supplier(Integer.parseInt(String.valueOf(map.get("supplier_id"))),
					String.valueOf(map.get("supplier_name")), String.valueOf(map.get("supplier_address")),
					String.valueOf(map.get("supplier_telephone")), String.valueOf(map.get("supplier_contact")));

		}
		return supplier;
	}

	@Override
	public Supplier getallSupplier() {
		String sql = "select * from supplier";
		List<Map<String, Object>> supplierlist = jdbcTemplate.queryForList(sql);
		Iterator<Map<String, Object>> iterator = supplierlist.iterator();
		while (iterator.hasNext()) {
			Map<String, Object> map = iterator.next();
			supplier = new Supplier(Integer.parseInt(String.valueOf(map.get("supplier_id"))),
					String.valueOf(map.get("supplier_name")), String.valueOf(map.get("supplier_address")),
					String.valueOf(map.get("supplier_telephone")), String.valueOf(map.get("supplier_contact")));

		}
		return supplier;
	}

	@Override
	public List<Supplier> getallSupplierforlist() {
		List<Supplier> supplierlists = new ArrayList<Supplier>();
		String sql = "select * from supplier";
		List<Map<String, Object>> supplierlist = jdbcTemplate.queryForList(sql);
		Iterator<Map<String, Object>> iterator = supplierlist.iterator();
		while (iterator.hasNext()) {
			Map<String, Object> map = iterator.next();
			supplier = new Supplier(Integer.parseInt(String.valueOf(map.get("supplier_id"))),
					String.valueOf(map.get("supplier_name")), String.valueOf(map.get("supplier_address")),
					String.valueOf(map.get("supplier_telephone")), String.valueOf(map.get("supplier_contact")));
			supplierlists.add(supplier);
		}
		return supplierlists;
	}

	@Override
	public Supplier selectSupplierById(int id) {
		Supplier supplier = null;
		String sql = "select * from supplier where supplier_id=?";
		List<Map<String, Object>> supplierlist = jdbcTemplate.queryForList(sql, new Object[] { id });
		Iterator<Map<String, Object>> iterator = supplierlist.iterator();
		while (iterator.hasNext()) {
			Map<String, Object> map = iterator.next();
			supplier = new Supplier(Integer.parseInt(map.get("supplier_id").toString()),
					map.get("supplier_name").toString(), map.get("supplier_address").toString(),
					map.get("supplier_telephone").toString(), map.get("supplier_contact").toString());

		}
		return supplier;
	}

	@Override
	public boolean updateSupplierInfoById(Supplier supplier) {
		String sql = "UPDATE supplier SET supplier_name = ?,supplier.supplier_address = ?,supplier_telephone = ?,supplier_contact = ? where supplier_id = ?;";
		int updatenumber = jdbcTemplate.update(sql,
				new Object[] { supplier.getSupplier_name(), supplier.getSupplier_address(),
						supplier.getSupplier_telephone(), supplier.getSupplier_contact(), supplier.getSupplier_id() });
		return updatenumber == 1 ? true : false;
	}

	@Override
	public boolean deleteSuppier(int supplierid) {
		String sql = "DELETE FROM supplier WHERE supplier_id = ?;";
		int delatenumber = jdbcTemplate.update(sql, new Object[] { supplierid });
		return delatenumber == 1 ? true : false;
	}

}
